function [cd,pd] = blsdelta(so,x,r,t,sig,q)  
%BLSDELTA Black-Scholes sensitivity to underlying price change.  
%   [CD,PD] = BLSDELTA(SO,X,R,T,SIG,Q) returns sensitivity in option value to  
%   change in the underlying security price.  Delta is also known as the hedge
%   ratio.  SO is the current stock price, X is the exercise price, R is the
%   risk-free interest rate, T is the time to maturity of the option in years, 
%   SIG is the standard deviation of the annualized continuously compounded 
%   rate of return of the stock (also known as the volatility), and Q is the
%   dividend rate or the foreign interest rate where applicable. The default
%   Q is 0.  CD is the delta of a call option, and PD is the delta of a put
%   option. 
%        
%   Note: 
%     This function uses normcdf, the normal cumulative distribution
%     function in the Statistics Toolbox. 
%  
%   For example, [c,p] = blsdelta(50,50,.1,.25,.3,0) returns  
%   c = 0.5955 and p = -0.4045.  
%  
%   See also BLSPRICE, BLSGAMMA, BLSTHETA, BLSRHO, BLSVEGA, BLSLAMBDA.  
%  
%   Reference: Options, Futures, and Other Derivative Securities, Hull,  
%              Chapter 13.  
  
%       Copyright 1995-2006 The MathWorks, Inc.
%       $Revision: 1.6.2.4 $   $Date: 2009/04/15 23:07:17 $  
   
if nargin < 5  
  error('finance:blsdelta:missingInputs','Missing one of SO, X, R, T, and SIG.')  
end  
if any(so <= 0 | x <= 0 | r < 0 | t <=0 | sig < 0)  
  error('finance:blsdelta:invalidInputs','Enter SO, X, and T > 0. Enter R and S >= 0.')  
end  
if nargin < 6  
  q = zeros(size(so)); % default dividend rate  
end  
  
message = blscheck('blsdelta', so, x, r, t, sig, q);
error(message);


% Perform scalar expansion & guarantee conforming arrays.
try
    [so, x, r, t, sig, q] = finargsz('scalar', so, x, r, t, sig, q);
catch
    error('Finance:blsdelta:InconsistentDimensions', ...
        'Inputs must be scalars or conforming matrices.')
end

% blspriceeng works with columns. Get sizes, turn to columns, run engine,
% and finally turn to arrays again:
[m, n] = size(so);

% Double up on fcn calls since blsprice calculates both calls and puts. Do
% this only if nargout>1
NumOpt = numel(so);
callSpec = {'call'};
callSpec = callSpec(ones(NumOpt,1));
putSpec = {};
if(nargout)>1
    putSpec = {'put'};
    putSpec = putSpec(ones(NumOpt,1));

    % double up the rest of the input args
    [so, x, r, t, sig, q] = deal([so(:);so(:)], [x(:);x(:)], [r(:);r(:)], ...
    [t(:);t(:)], [sig(:);sig(:)], [q(:);q(:)]);
end
OptSpec = [callSpec;putSpec];
OutSpec = {'delta'};




% call eng fuction
delta = blspriceeng(OutSpec, OptSpec, so, x, r, t, sig, q);

% Now separate calls from puts
cd=reshape(delta{1}(1:NumOpt), m, n);
if(nargout>1)
    pd = reshape(delta{1}(NumOpt+1:end), m, n);
end